package com.ljkj.cordial.chat.widget;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.adapter.ListPopAdapter;
import com.nic.view.widget.CommonPopupWindow;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 底部弹出框
 */
public class BottomListPop extends CommonPopupWindow {
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    private ListPopAdapter listPopAdapter;

    public BottomListPop(Context c) {
        super(c, R.layout.dialog_bottom_list_pop, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ButterKnife.bind(this, contentView);
        listPopAdapter = new ListPopAdapter();
        listView.setAdapter(listPopAdapter);
        getPopupWindow().setOutsideTouchable(true);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPopupWindow().dismiss();
            }
        });
    }

    public BottomListPop setList(List<String> list, AdapterView.OnItemClickListener listener) {
        listPopAdapter.setList(list);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                getPopupWindow().dismiss();
                if (listener != null) {
                    listener.onItemClick(parent, view, position, id);
                }
            }
        });
        return this;
    }

    public BottomListPop show() {
        showBottom(((Activity) context).getWindow().getDecorView());
        return this;
    }
}
